<%@page import="BusinessLogic.Result.ResultType"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="BusinessLogic.Result" %>
<%@ page import="BusinessLogic.Course" %>
<%@ page import="BusinessLogic.Session" %>
<%@ page import="com.oreilly.servlet.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.net.URLEncoder" %>
<title>EasySchedule</title>
<link href="public/style.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="public/jquery.js"></script>
<script type="text/javascript" src="public/courseValidations.js"></script>
<script type="text/javascript">
	function goToAcnhor() { 
		window.location.hash="errorAnchor"; 
	}
	function handleDeleteResponse(data){
		data=data+''; 
		if (data.indexOf("success",0)<0) 
			alert(data);
		else 
			$('#'+day+start+end).remove();
	}
	function deleteSession(day,start,end){
		var answer = confirm ("Are you sure you want to delete this session?");
		if (answer)
			{
			var cName = '<%=request.getParameter("NAME")%>';
			var admin = '<%=request.getParameter("ADMIN")%>';
			$.get(
				    "/tamirandevyatar/requestHandler",
				    { action: "deleteCourseSession", COURSE_NAME:cName, ADMIN:admin, DAY: day, START_TIME: start, END_TIME: end},
					function(data){
						data=data+''; 
						if (data.indexOf("success",0)<0) {
							alert(data);
				    	}
						else {
							$('#'+day+start+end).remove();
						}
					},
				    "html"
				);
			}
	}
	function addSession(){
		var cName = '<%=request.getParameter("NAME")%>';
		var admin = '<%=request.getParameter("ADMIN")%>';
		window.location.href = "./addSession?NAME="+cName+'&'+"ADMIN="+admin;
	}
</script>

<%
/*

*/%>
<jsp:useBean id="requestHandler" class="RequestHandlerPackage.RequestHandler" scope="application"/>
<%
String cName = request.getParameter("NAME");
Map<String, String[]> params = new HashMap<String,String[]>();
params.put("action", new String[]{"getCourse"});
params.put("NAME", new String[]{cName});
Result res = requestHandler.handle(params);
Course course = null;
if (res.getResultType().equals(ResultType.SUCCESS))
{
	course = (Course)res.getSuccessReturnValue();
}
else{%>
	<script type="text/javascript">
	alert("The DB encountered a problem, please try again.");
	</script>
<%}
params.clear();
params.put("action", new String[]{"getSessionsByCourse"});
params.put("NAME", new String[]{cName});
res = requestHandler.handle(params);
List<Session> sessions = null;
if (res.getResultType().equals(ResultType.SUCCESS))
{
	sessions = (List<Session>)res.getSuccessReturnValue();
}
else{%>
	<script type="text/javascript">
	alert("The DB encountered a problem, please try again.");
	</script>
<%}%>

</head>

<body>
   <div id="page">
	        <div id="header">
	            <h1><a href="./">Easy<span class="logo">Schedule</span></a></h1>
	            <p id="subtitle">Nice and easy registration system</p>
	        </div>
	        <div id="menu">
	            <ul>
	                <li class="left_menu"><a href="./">Home</a></li>
	                <li class="left_menu"><a href="./about.html">About</a></li>
	                <li class="left_menu"><a href="/tamirandevyatar/logout">Logout</a></li>
	            </ul>
	        </div>
        <div id="main">	
        	<div id="sidebar">
                <div class="box" id="login">
                    <h2 >Menu</h2>
                    <div class="box2">
                        <ul>
                            <li><a href="/tamirandevyatar/allCourses">Show courses</a></li>
                            <li><a href="/tamirandevyatar/addCourse">Add course</a></li>
                            <li><a href="/tamirandevyatar/users">Manage users</a></li>
                            <li><a href="/tamirandevyatar/update">Update my details</a></li>
                        </ul>
                    </div>
                </div><!-- box -->				
            </div><!-- sidebar -->    	              
            <div id="content">
                <div id="contheader">
                    <h2>Editing Course</h2>
                </div>		
                <div id="content2" class="welcome">	
					<div class="entry" id="<%=course.getNameNoSpaces()%>">
						<h3 class="course"><%=cName%></h3>
						<button onclick="addSession()">Add session</button>
						<p><span class="red">Description:</span> <%=course.getDescription()%></p>
						<p><span class="red">Capacity:</span> <%=course.getCapacity()%></p>
						<p><span class="red">Group:</span> <%=course.getGroup()%></p>
						<p><span class="red">Credit points:</span> <%=course.getPoints()%></p>
						<h4>Sessions:</h4>
						<%for (Session s : sessions){
						String day = s.getDay(); 
						String start = ((Integer)s.getStart_time()).toString();  
						String end = ((Integer)s.getEnd_time()).toString();%>
						<div id="<%=day+start+end %>">
							  <p><span class="red">day:</span> <%=day%></p>
							  <p><span class="red">Start time:</span> <%=start+" :00" %></p>
							  <p><span class="red">End time:</span> <%=end+" :00" %></p>
							  <button onclick="deleteSession('<%=day%>','<%=start%>','<%=end%>')">delete session</button>
						</div>
						<%}%>

					</div>
                </div><!-- content2 -->								              
            </div><!-- content -->                    
            <div class="clearing">&nbsp;</div> 
            <div id="footer">
                <p><a href="http://www.cs.technion.ac.il/~cs236369/">Managing Data on the World-Wide Web</a></p>
            </div>
        </div><!-- main -->
    </div><!-- page -->
	</body>
</html>
